package com.mz.common.config;

import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


/**
 * springboot 处理返回值 long 转 string
 * */
@Configuration
public class JacksonConfig {

    /**
     * Jackson 全局转化为 Long 类型为 String, 解决 jackson 序列化时 Long 类型缺失精度的问题。
     * @return Jackson2ObjectMapperBuilderCustomizer 注入的对象。
     * */
    @Bean
    public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer(){
        return jacksonObjectMapperBuilder -> jacksonObjectMapperBuilder
                .serializerByType(Long.class, ToStringSerializer.instance)
                .serializerByType(Long.TYPE, ToStringSerializer.instance);
    }

}
